package com.ikkong.biz.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import com.google.common.collect.Sets;
import com.ikkong.biz.BizCst;
import com.ikkong.biz.meta.intercept.AuthTagIntercept;
import com.ikkong.core.constant.ConstCache;
import com.ikkong.core.constant.ConstShiro;
import com.ikkong.core.jfinal.ext.kit.JStrKit;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ShiroKit;
import com.ikkong.core.toolbox.Record;
import com.ikkong.core.toolbox.kit.CollectionKit;
import com.ikkong.system.controller.base.AdminBaseController;
import com.ikkong.biz.model.AuthTag;
import com.ikkong.biz.model.Teacher;
import com.ikkong.biz.service.AuthTagService;
import com.ikkong.biz.service.impl.AuthTagServiceImpl;
import com.jfinal.plugin.ehcache.CacheKit;

/**
 * Generated by Blade.
 * 2016-11-21 10:43:47
 */
public class AuthTagController extends AdminBaseController {
	private static String CODE = "authTag";
	private static String PERFIX = "tb_auth_tag";
	private static String LIST_SOURCE = "AuthTag.list";
	private static String BASE_PATH = "/biz/authTag/";
	
	AuthTagService service = new AuthTagServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "authTag.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "authTag_add.html");
	}

	public void getStuTree(){
        String val = getPara("val");
        List<Record> list = service.getTreeList(BizCst.STU_TYPE,val);
        renderJson(json(list));
    }

	public void getParTree(){
        String val = getPara("val");
        List<Record> list = service.getTreeList(BizCst.PAR_TYPE, val);
        renderJson(json(list));
    }

	public void getTeaTree(){
        String val = getPara("val");
        List<Record> list = service.getTeaTreeList(val);
        renderJson(json(list));
    }
	
	public void getNoticeStuTree(){
		Teacher teacher = ShiroKit.getTeacher();
        if(teacher != null){
        	List<Record> list = service.getNoticeStuList(3, teacher.getId());
            renderJson(json(list));
        }else{
        	List<Record> list = service.getNoticeStuList(3, 0);
            renderJson(json(list));
        }
	}
	
	public void getNoticeParTree(){
		Teacher teacher = ShiroKit.getTeacher();
        if(teacher != null){
        	List<Record> list = service.getNoticeStuList(2, teacher.getId());
            renderJson(json(list));
        }else{
        	List<Record> list = service.getNoticeStuList(3, 0);
            renderJson(json(list));
        }
	}

	public void getNoticeTree(){
        Teacher teacher = ShiroKit.getTeacher();
        if(teacher != null){

            List<Record> list = service.getNoticeTreeList(teacher.getId());
            renderJson(json(list));
        }else{
        	renderJson(json(new ArrayList<>()));
        }
    }


	public void edit() {
		String id = getPara(0);
		AuthTag authTag = service.findById(id);
		setAttr("id", id);
        setAttr("model", JsonKit.toJson(authTag));
        setAttr("item", authTag);
        setAttr("code", CODE);
		render(BASE_PATH + "authTag_edit.html");
	}

	public void view() {
		String id = getPara(0);
		AuthTag authTag = service.findById(id);
		setAttr("model", JsonKit.toJson(authTag));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "authTag_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE,new AuthTagIntercept());
		renderJson(grid);
	}

	private String removeDuplicates(String origin){
        if (JStrKit.isBlank(origin)){
            return "";
        }
        Set<String> set = Sets.newHashSet();
        for (String s: origin.split(",")
             ) {
            set.add(s);
        }
        return CollectionKit.join(set,",");
    }

	public void save() {
		AuthTag authTag = mapping(PERFIX, AuthTag.class);
        authTag.setVersion(0);
        authTag.setCtime(new Date());
        authTag.setTeacher_auth(JStrKit.wrapWithDelimiter(removeDuplicates(authTag.getTeacher_auth())));
        authTag.setParent_auth(JStrKit.wrapWithDelimiter(authTag.getParent_auth()));
        authTag.setStudent_auth(JStrKit.wrapWithDelimiter(authTag.getStudent_auth()));
        authTag.setType(1);
		boolean temp = service.save(authTag);
		CacheKit.removeAll(ConstCache.DICT_CACHE);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		AuthTag authTag = mapping(PERFIX, AuthTag.class);
		AuthTag db = service.findById(authTag.getId());
		db.setTeacher_auth(JStrKit.wrapWithDelimiter(removeDuplicates(authTag.getTeacher_auth())));
		db.setParent_auth(JStrKit.wrapWithDelimiter(authTag.getParent_auth()));
		db.setStudent_auth(JStrKit.wrapWithDelimiter(authTag.getStudent_auth()));
		db.setUtime(new Date());
		db.setVersion(authTag.getVersion()+1);
		db.setName(authTag.getName());
		//更新所e有字段，必须先取再更新
		boolean temp = service.updateEveryCol(db);
		CacheKit.removeAll(ConstCache.DICT_CACHE);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
